﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.Base.UserRole;
using UFIDA.U9.Base.WorkCalendar;
using UFIDA.U9.CBO.MFG.WorkCenter;
using UFIDA.U9.MO.SFC.DispatchCO;
using UFIDA.U9.MO.SFC.DispatchCompleteOrder;
using UFIDA.U9.MO.SFC.DispatchOrder;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class DispatchCompleteOrderBase
    {
        public static DispatchCompleteOrderDoc ApproveDoc(DispatchCompleteOrderDoc doc)
        {
            ApproveDispatchCOBP bp = new ApproveDispatchCOBP();
            bp.CurSysVersion = (int)doc.SysVersion;
            bp.DocKey = doc.Key;
            bp.Do();

            return doc;
        }

        public static DispatchCompleteOrderDoc CreateDoc(DispatchCompleteOrderAITDTOData dto)
        {
            DispatchCompleteOrderDoc doc;
            using (ISession session = Session.Open())
            {
                doc = DispatchCompleteOrderDoc.Create();
                CreateHead(doc, dto);
                CreateLine(doc, dto);

                session.Commit();
            }

            return doc;
        }

        private static void CreateLine(DispatchCompleteOrderDoc doc, DispatchCompleteOrderAITDTOData dto)
        {
            //缓存派工单行
            Dictionary<int, DispatchOrderDocLine> disdic = new Dictionary<int, DispatchOrderDocLine>();
            foreach (var operline in doc.DispatchOrder.DispatchOrderDocLines)
            {
                disdic.Add(operline.LineNum, operline);
            }

            //创建行
            foreach (var dtoline in dto.DispatchCompleteOrderDocLines)
            {
                DispatchCompleteOrderDocLine line = DispatchCompleteOrderDocLine.Create(doc);
                line.LineNum = dtoline.LineNum;
                line.DispatchOrderLine = disdic[dtoline.DispatchOrderLine];
                if (line.DispatchOrderLine == null)
                    throw new Exception($"派工单[{dto.DispatchOrder.Code}]行[{dtoline.DispatchOrderLine}]不存在!");
                //line.DispatchCompleteOrderDoc = line.DispatchOrderLine.DispatchOrderDoc;
                line.TaskCode = line.DispatchOrderLine.TaskCode;
                line.ProductUOM = line.DispatchOrderLine.ProductUOM;
                line.MOOperation = line.DispatchOrderLine.MOOperation;//订单工序
                line.MO = line.DispatchOrderLine.MO;//生产订单
                line.EligibleQty = dtoline.EligibleQty;//合格数量
                line.CompleteQty = dtoline.EligibleQty;//完工数量
                line.DesOperation = line.DispatchOrderLine.MOOperation.NextOperation;//标准工序

                //这里不再赋值,自动生成 工时维护 实体 
                line.RealLaborPrepareHours = dtoline.RealLaborPrepareHours;//实际人工准备工时
                line.RealLaborProcessHours = dtoline.RealLaborProcessHours;//实际人工加工工时
                line.RealMachinePrepareHours = dtoline.RealMachinePrepareHours;//实际机器准备工时
                line.RealMachineProcessHours = dtoline.RealMachineProcessHours;//实际机器加工工时

                line.AutoTransferNextOp = true;
                line.ActualStartDate = doc.ActualStartDate;
                line.ActualEndDate = doc.ActualEndDate;
            }
        }

        private static void CreateHead(DispatchCompleteOrderDoc doc, DispatchCompleteOrderAITDTOData dto)
        {
            if (dto.DispatchOrder == null)
                throw new Exception("DispatchOrder不能为空!");
            if (String.IsNullOrEmpty(dto.ActualStartDate))
                throw new Exception("ActualStartDate不能为空");
            if (String.IsNullOrEmpty(dto.ActualEndDate))
                throw new Exception("ActualEndDate不能为空!");

            doc.DocNo = dto.DocNo;
            doc.DispatchOrder = EntityUtils.GetDocEntity<DispatchOrderDoc>(dto.DispatchOrder.Code);
            doc.Org = doc.DispatchOrder.Org;
            doc.MO = doc.DispatchOrder.MO;
            doc.DispatchCompleteOrderDocType = EntityUtils.GetEntityByCode<DispatchCompleteOrderDocType>(dto.DispatchCompleteOrderDocType);
            doc.BusinessDate = DateTime.Parse(dto.BusinessDate);
            doc.BusinessCreatedOn = doc.BusinessDate;
            doc.BusinessCreatedBy = User.Finder.FindByID(Context.LoginUserID);
            doc.ActualStartDate = DateTime.Parse(dto.ActualStartDate);
            doc.ActualEndDate = DateTime.Parse(dto.ActualEndDate);
            doc.RptWorkCenter = EntityUtils.GetEntityByCode<WorkCenter>(dto.RptWorkCenter); //工作中心
            doc.WorkFlowID = Guid.Empty;
            doc.StateMachineID = Guid.Empty;
        }
    }
}

